#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
MainWindow::~MainWindow()
{

}

void MainWindow::init_data()
{
    num1 = 0;
    num2 = 0;
    operat_flag = 0;
}

void MainWindow::readNum(int num)
{
      if(operat_flag == 0){
          num1 = num1 * 10 + num;
      }
      else{
          num2 = num2 * 10 + num;
      }
}

void MainWindow::operation()
{
    switch(operat){
    case 1:
        result = num1 + num2;
        break;
    case 2:
        result = num1 - num2;
        break;
    case 3:
        result = num1 * num2;
        break;
    case 4:
        result = num1 / num2;
        break;
    }
    num1 = result;
    num2 = 0;
    s = QString::number(result,10);
}




void MainWindow::on_button_0_clicked()
{
    s += "0";
    data->setText(s);
    readNum(0);
}

void MainWindow::on_button_1_clicked()
{
    s += "1";
    data->setText(s);
    readNum(1);
}

void MainWindow::on_button_2_clicked()
{
    s += "2";
    data->setText(s);
    readNum(2);
}


void MainWindow::on_button_3_clicked()
{
    s += "3";
    data->setText(s);
    readNum(3);
}

void MainWindow::on_button_4_clicked()
{
    s += "4";
    data->setText(s);
    readNum(4);
}

void MainWindow::on_button_5_clicked()
{
    s += "5";
    data->setText(s);
    readNum(5);
}

void MainWindow::on_button_6_clicked()
{
    s += "6";
    data->setText(s);
    readNum(6);
}

void MainWindow::on_button_7_clicked()
{
    s += "7";
    data->setText(s);
    readNum(7);
}

void MainWindow::on_button_8_clicked()
{
    s += "8";
    data->setText(s);
    readNum(8);
}

void MainWindow::on_button_9_clicked()
{
    s += "9";
    data->setText(s);
    readNum(9);
}

void MainWindow::on_button_ce_clicked()
{
    s = "";
    data->setText("");
    init_data();
}

void MainWindow::on_button_jia_clicked()
{
    s += "+";
    data->setText(s);
    operat_flag = 1;
    operat = 1;
}

void MainWindow::on_button_jian_clicked()
{
    s += "-";
    data->setText(s);
    operat_flag = 1;
    operat = 2;
}

void MainWindow::on_button_cheng_clicked()
{
    s += "×";
    data->setText(s);
    operat_flag = 1;
    operat = 3;
}

void MainWindow::on_button_chu_clicked()
{
    s += "÷";
    data->setText(s);
    operat_flag = 1;
    operat = 4;
}

void MainWindow::on_button_result_clicked()
{
    operation();
    QString a = QString::number(result,10);
    data->setText(a);
}
